package 优先算法.二分查找.寻找峰值;

/**
 * https://leetcode.cn/problems/find-peak-element/description/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {1,2,1,3,5,6,4};
        int rel = so.findPeakElement(arr);
        System.out.println(rel);
    }
}
class Solution {
    //左侧查找
    public int findPeakElement(int[] nums) {
        int left = 0,right = nums.length-1;
        if(nums.length == 1){
            return left;
        }
        if(nums[left] > nums[left+1]){
            return left;
        }
        if(nums[right] > nums[right - 1]){
            return right;
        }
        while(left < right){
            int mid = left + (right - left + 1)/2;
            if(nums[mid-1] < nums[mid]){
                left = mid;
            }
            if(nums[mid] < nums[mid-1]){
                right = mid - 1;
            }
        }
        return left;
    }
}
class Solution1 {
    //右侧查找
    public int findPeakElement(int[] nums) {
        int left = 0,right = nums.length-1;
        if(nums.length == 1){
            return left;
        }
        if(nums[left] > nums[left+1]){
            return left;
        }
        if(nums[right] > nums[right - 1]){
            return right;
        }
        while(left < right){
            int mid = left + (right - left)/2;
            if(nums[mid] > nums[mid+1]){
                right = mid;
            }
            if(nums[mid] < nums[mid+1]){
                left = mid + 1;
            }
        }
        return left;
    }
}